package org.bim.view.drag.design.controller;


import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.bim.view.drag.design.api.req.VisualCategoryReq;
import org.bim.view.drag.design.api.vo.GroupVisualCategoryVO;
import org.bim.view.drag.design.api.vo.TUserVO;
import org.bim.view.drag.design.constant.CommonConstant;
import org.bim.view.drag.design.dao.VisualCategoryMapper;
import org.bim.view.drag.design.dao.VisualMapper;
import org.bim.view.drag.design.entity.TVisual;
import org.bim.view.drag.design.entity.TVisualCategory;
import org.bim.view.drag.design.exception.BizCommonException;
import org.bim.view.drag.design.model.HttpResult;
import org.bim.view.drag.design.model.Page;
import org.bim.view.drag.design.util.AssertUtil;
import org.bim.view.drag.design.util.PageUtil;
import org.bim.view.drag.design.util.ThreadLocalUtil;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @author pengxg
 * @date 2021/12/16 2:44 下午
 */
@Api
@AllArgsConstructor
@RestController
@RequestMapping("/category")
public class VisualCategoryController {
    private VisualCategoryMapper visualCategoryMapper;
    private VisualMapper visualMapper;

    @GetMapping("detail")
    public HttpResult<TVisualCategory> detail(TVisualCategory req) {
        return HttpResult.success(visualCategoryMapper.selectOne(req));
    }

    @GetMapping("list")
    public HttpResult<List<TVisualCategory>> list(@RequestParam(required = false) String projectId) {
        List<TVisualCategory> result = getList(projectId);
        return HttpResult.success(result);
    }

    @GetMapping("list-from-users")
    public HttpResult<List<GroupVisualCategoryVO>> listFromUsers() {
        List<GroupVisualCategoryVO> result =new ArrayList<>();
        List<TVisualCategory> adminList = getList(null);
        GroupVisualCategoryVO vo =new GroupVisualCategoryVO();
        vo.setName("系統");
        vo.setId("-1");
        vo.setCategoryList(adminList);
        result.add(vo);
        List<GroupVisualCategoryVO> userList = visualCategoryMapper.findFromUsers();
        result.addAll(userList);
        return HttpResult.success(result);
    }

    @GetMapping("list-from-project")
    public HttpResult<List<GroupVisualCategoryVO>> listFromProject() {
        List<GroupVisualCategoryVO> result = visualCategoryMapper.findFromProject();
        return HttpResult.success(result);
    }

    private List<TVisualCategory> getList(String projectId) {
        List<TVisualCategory> result = new ArrayList<>();
        // 区分不同用户
        TUserVO userVO = ThreadLocalUtil.getCurrentUser();
        Example example = new Example(TVisualCategory.class, false);
        Example.Criteria criteria = example.createCriteria()
                .andEqualTo("isDeleted", false);
        if (userVO.isAdmin()) {
            if (StringUtils.isEmpty(projectId)) {
                criteria.andIsNull("ownerId")
                        .orEqualTo("ownerId", userVO.getId());
            } else {
                criteria
                        .andEqualTo("projectId", projectId);
            }
            result = visualCategoryMapper.selectByExample(example);
        } else {
            criteria
                    .andEqualTo("projectId", projectId);
            if (StringUtils.isEmpty(projectId)) {
                criteria.andEqualTo("ownerId", userVO.getId())
                        .andIsNull("projectId");
            }
            result = visualCategoryMapper.selectByExample(example);
        }
        return result;
    }

    @GetMapping("page")
    public HttpResult<Page<TVisualCategory>> page(VisualCategoryReq req) {
        Page<TVisualCategory> page = PageUtil.doPage(req.toCondition(), () -> getList(req.getProjectId()));
        return HttpResult.success(page);
    }

    @PostMapping("save")
    public HttpResult<String> save(@RequestBody TVisualCategory form) {
        TUserVO currentUser = ThreadLocalUtil.getCurrentUser();
        form.setOwnerId(currentUser.getId());
        visualCategoryMapper.insertSelective(form);
        return HttpResult.success(form.getId());
    }

    @PostMapping("update")
    public HttpResult<String> update(@RequestBody TVisualCategory form) {
        visualCategoryMapper.updateByPrimaryKeySelective(form);
        return HttpResult.success(form.getId());
    }

    @PostMapping("remove")
    public HttpResult remove(@RequestParam String ids) {
        String[] idList = ids.split(",");
        for (String id : idList) {
            TVisualCategory category =  visualCategoryMapper.selectByPrimaryKey(id);
            AssertUtil.isTrue(category!=null,"分类不存在");
            Example example = new Example(TVisual.class, false, false);
            example.createCriteria().andEqualTo("category", id);
            int num = visualMapper.selectCountByExample(example);
            AssertUtil.isTrue(num == 0, new BizCommonException("该分类下含有大屏，不能删除！"));
            category.setIsDeleted(true);
            visualCategoryMapper.updateByPrimaryKeySelective(category);
        }
        return HttpResult.success();
    }
}
